layui.define(['table', 'form', 'admin'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , admin = layui.admin;

    //显示世界杯赛事列表
    table.render({
        elem: '#LAY-worldcup-match'
        , url: '/admin/worldcup/getlist'
        , where: {
            date: $('.date_list p.active').data('day')
        }
        , cols: [[
            { field: 'match_id', width: 120, title: '赛事id', align: "center" }
            , { field: 'group_code', title: '分组', align: "center" }
            , {
                field: '', title: '比赛状态', align: "center", templet: function (d) {
                    return d.match_info ? d.match_info.match_detail_status : ''
                }
            }
            , { field: 'match_hour', title: '比赛时间', align: "center" }
            , {
                field: '', title: '比赛场地', align: "center", templet: function (d) {
                    return '<p class="venue_name">' + d.venue_name + '</br>' + d.venue_name_en + '</p>'
                }
            }
            , {
                field: '', align: "center", title: '球队', templet: function (d) {
                    var html = d.home_team_name + '&nbsp;&nbsp;<img src="' + d.home_team_logo + '" height="25">';
                    html += '<strong> VS </strong>';
                    html += '<img src="' + d.away_team_logo + '" height="25">&nbsp;&nbsp;' + d.away_team_name;
                    return html;
                }
            }
            // , {title: '操作', width: 250, align: 'center', fixed: 'right', toolbar: '#table-worldcup-match'}
        ]]
        , page: false
        , limit: 200
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    $('.date_list p').click(function () {
        $('.date_list p').removeClass('active')
        $(this).addClass('active')
        //执行重载
        table.reload('LAY-worldcup-match', {
            where: {
                date: $('.date_list p.active').data('day')
            }
            , page: {
                curr: 1   //默认从第一页开始搜索
            },
        });
    })


    form.on("submit(worldcup-match-add)", function (obj) {
        layer.load();
        //提交
        admin.req({
            url: "/admin/worldcup/add",
            data: obj.field,
            success: function (res) {
                layer.closeAll("loading");
                if (res.code == 0) {
                    layer.msg(res.msg, { icon: 1 });
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        parent.window.location.reload();
                        // parent.layui.table.render(getWorldcupMatchList());
                    }, 1200);
                }
            },
            complete: function () {
                layer.closeAll("loading");
            },
        });
        return false;
    });


    // 积分分组选择
    $('.timeChoice button').click(function () {
        $('.timeChoice button').removeClass('btn_danger');
        $(this).addClass('btn_danger');
        //执行重载
        table.reload('LAY-worldcup-point', {
            where: {
                group: $('.timeChoice .btn_danger').data('group')
            }
        });
    })

    // 积分分组列表
    table.render({
        elem: '#LAY-worldcup-point'
        , url: '/admin/worldcup/point_list'
        , where: {
            group: $('.timeChoice .btn_danger').data('group')
        }
        , cols: [[
            { type: 'numbers', title: '序号', width: 70 }
            , {
                field: '', title: '球队', templet: function (d) {
                    let html = '';
                    if (d.team_logo) {
                        html += '<img src="' + d.team_logo + '" height="25"> &nbsp;&nbsp;'
                    }
                    return html + d.team_name
                }
            }
            , { field: 'match_times', title: '场次' }
            , {
                field: 'date', title: '胜平负', templet: function (d) {
                    return d.win + ' / ' + d.draw + '/' + d.lost;
                }
            }
            , {
                field: 'date', width: 150, title: '进失球', templet: function (d) {
                    return d.goals + ' / ' + d.goals_conceded;
                }
            }
            , { field: 'goal_difference', width: 150, title: '净胜球' }
            , {
                field: 'date', width: 150, title: '场均进失球', templet: function (d) {
                    return d.goal_rate + ' / ' + d.fumble_rate;
                }
            }
            , { field: 'goal_difference_rate', width: 150, title: '场均净胜球' }
            , { field: 'score', width: 150, title: '积分' }
            , { title: '操作', width: 120, align: 'center', fixed: 'right', toolbar: '#table-worldcup-point' }
        ]]
        , page: false
        , limit: 40
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });


    table.on("tool(LAY-worldcup-point)", function (obj) {
        if (obj.event == "edit") {
            layer.open({
                type: 2,
                title: "编辑积分",
                content: "/admin/worldcup/point_edit?id=" + obj.data.id,
                maxmin: true,
                area: ["700px", "630px"],
            });
        }
    });

    form.on("submit(worldcup-point-edit)", function (obj) {
        layer.load();
        //提交
        admin.req({
            url: "/admin/worldcup/point_edit",
            data: obj.field,
            success: function (res) {
                layer.closeAll("loading");
                if (res.code == 0) {
                    layer.msg(res.msg, { icon: 1 });
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        //执行重载
                        parent.layui.table.reload('LAY-worldcup-point', {
                            where: {
                                group: $('.timeChoice .btn_danger').data('group')
                            }
                        });
                    }, 1200);
                }
            },
            complete: function () {
                layer.closeAll("loading");
            },
        });
        return false;
    });

    form.on("submit(worldcup-point-add)", function (obj) {
        layer.load();
        //提交
        admin.req({
            url: "/admin/worldcup/point_add_handle",
            data: obj.field,
            success: function (res) {
                layer.closeAll("loading");
                if (res.code == 0) {
                    layer.msg(res.msg, { icon: 1 });
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        //执行重载
                        parent.layui.table.reload('LAY-worldcup-point', {
                            where: {
                                group: $('.timeChoice .btn_danger').data('group')
                            }
                        });
                    }, 1200);
                }
            },
            complete: function () {
                layer.closeAll("loading");
            },
        });
        return false;
    });

    form.on("submit(worldcup-rank-add)", function (obj) {
        layer.load();
        //提交
        admin.req({
            url: "/admin/worldcup/rank_add_handle",
            data: obj.field,
            success: function (res) {
                layer.closeAll("loading");
                if (res.code == 0) {
                    layer.msg(res.msg, { icon: 1 });
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                        //执行重载
                        parent.layui.table.reload('LAY-worldcup-point', {
                            where: {
                                group: $('.timeChoice .btn_danger').data('group')
                            }
                        });
                    }, 1200);
                }
            },
            complete: function () {
                layer.closeAll("loading");
            },
        });
        return false;
    });


    exports('worldcup', {})
});